package com.blog.back.service.impl;

import com.blog.back.dao.ext.ClassificationExtMapper;
import com.blog.back.redis.RedisUtils;
import com.blog.utils.PageUtils;
import com.blog.utils.Query;
import com.blog.utils.RedisKeys;
import com.blog.vo.ArticleClassificationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.blog.back.dao.ClassificationDao;
import com.blog.entity.ClassificationEntity;
import com.blog.back.service.ClassificationService;


@Service("classificationService")
public class ClassificationServiceImpl extends ServiceImpl<ClassificationDao, ClassificationEntity> implements ClassificationService {

    @Autowired
    ClassificationDao classificationDao;

    @Autowired
    ClassificationExtMapper classificationExtMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ClassificationEntity> page = this.page(
                new Query<ClassificationEntity>().getPage(params),
                new QueryWrapper<ClassificationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<ArticleClassificationVO> findClassAndArticleByUsersId(Long usersId) {
        //TODO 想办法把这玩意加进缓存里

        //查询
        List<ArticleClassificationVO.ArticleClassificationItemVO> list = classificationExtMapper.selectArticleByClassByUsersId(usersId);
        //根据分类名分组
        Map<String, List<ArticleClassificationVO.ArticleClassificationItemVO>> map = list.stream().collect(Collectors.groupingBy(s -> s.getClassContent()));
        //组装数据
        List<ArticleClassificationVO> vos = new ArrayList<>(map.size());
        for (String classContent : map.keySet()) {
            ArticleClassificationVO vo = new ArticleClassificationVO()
                    .setClassContent(classContent)
                    .setList(map.get(classContent));
            vos.add(vo);
        }

        return vos;
    }

}